import { observable, action } from 'mobx';
import {WeaForm,WeaTableNew} from 'comsMobx'
import {WeaLocaleProvider} from 'ecCom';
import { message, Modal } from "antd"
const { TableStore } = WeaTableNew;
const confirm = Modal.confirm;
import * as Prj_Apis from "../apis/project"
import * as  Apis from '../apis/project';
import {getFormInitDatas} from "../util/index"
import { RelateListStore } from './taskListStore';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * @author ljc 2017-12-20
 * 项目的编辑，新增等
 */
export  class ProjectInfoStore{
    @observable title = getLabel(83808,"编辑项目");

	@observable rightMenu = [];
	@observable prjid = "";
    @observable prjForm = new WeaForm();
    @observable visible = false;
    @observable prjInfo = [];
    @observable baseParams = {}  ; //基本数据保存
    @observable validateRules = {};

	@observable prjLogTableStore = new TableStore();  //日志

	@observable prjFieldLogTableStore = new TableStore();  //字段日志

	@observable prjFieldDetailLogTableStore = new TableStore();  //字段详情日志

	@observable fieldLogVisible = false;

	@observable fieldDetailLogVisible = false;

	@observable fieldname = '';

    @observable field = '';
    
    @observable isreload = false;

    @observable isApprove = false;

    @observable templatename = "";//模板名称

    @observable prjname = "";

    @observable doSaveVisible = false;
   
    @action 
    handleDialog = (bool,id,params={} ) =>{
		this.visible = bool;
		if(bool){
            this.prjid = id;
            this.baseParams =params ;
            this.getPrjInfo(params);
		}
    }
    
    getPrjInfo=(params={})=>{
        this.prjForm = new WeaForm();
        const newParams = {prjid:this.prjid,...params}
		Prj_Apis.getPrjInfo(newParams).then(data=>{
            this.isApprove = data.isApprove;
            this.prjInfo = data.fieldinfo;
            this.prjname = data.prjname;
            this.rightMenu = data.rightMenus;
            this.prjForm && !this.prjForm.isFormInit && this.prjForm.initFormFields(data.fieldinfo);
			this.prjForm.isFormInit && this.prjForm.updateFields(getFormInitDatas(data.fieldinfo),false);
		})

	}

    setFormFields=(value)=>{
        // console.log(value);
		this.prjForm.updateFields(value, false);    //true代表完全覆盖方式更新条件值
    }

    //保存规则
	setValidate=(params={})=>{
		this.validateRules = params;
    }

    //项目执行操作
    @action
    doPlanOpt=(params={})=>{
        Prj_Apis.doPlanOpt(params).then(data=>{
			this.getPrjInfo({viewtype:"view"});
		});
    }

    //项目卡片另存为模板
	@action
	doSaveTemplet = (params = {}) => {
		Prj_Apis.doSaveTemplet({
			prjname:this.templatename,
			prjid:this.prjid
			}).then(data => {
			if (data.success) {
				Modal.success({
					title: getLabel(15172, "系统提示"),
					content: getLabel(19468, "另存为模板") + getLabel(84565, "成功！"),
				});
			} else {
				Modal.error({
					title: getLabel(15172, "系统提示"),
					content: getLabel(19468, "另存为模板") + getLabel(84566, "失败！"),
				});
			}
			this.setVisiable(false);
		});
	}
    
    //项目删除
    @action
    delPrjInfo=(params={})=>{
        let _this = this;
        Modal.confirm({
            title: getLabel(15172,"系统提示"),
            content: getLabel(383860,"您确认要删除项目吗？"),
            onOk() {
                Prj_Apis.delPrjInfo(params).then(data=>{
					message.success(getLabel(83472, "删除成功！"));
					if(params.from!='portal'){
						_this.visible = false;
						window._table.reLoad();
					}else{
						if(params.callBack){
							params.callBack();
						}
					}
                });
            },
            onCancel() { },
        })
    }
    
    getPrjLogList = (params = {}) => {
		Apis.getPrjLogList(params).then(data => {
			this.prjLogTableStore.getDatas(data.sessionkey, 1);
		})
	}

	getPrjFieldLogList = (params = {}) => {
		Apis.getPrjFieldLogList(params).then(data => {
			this.prjFieldLogTableStore.getDatas(data.sessionkey, 1);
		})
	}

	getPrjFieldDetaiLogList = (params = {}) => {
		Apis.getPrjFieldDetaiLogList(params).then(data => {
			this.prjFieldDetailLogTableStore.getDatas(data.sessionkey, 1);
		})
	}

	@action
	setFieldLogVisible = (v) =>{
		this.fieldLogVisible = v;
	}

	@action
	setFieldDetailLogVisible = (v) =>{
		this.fieldDetailLogVisible = v;
	}

	@action
	setFieldName = (v) =>{
		this.fieldname = v;
    }
    
    @action
	setField = (v) =>{
		this.field = v;
    }
    
    @action
	setLoaded = () => {
		this.isreload = true;
    }
    
    setVisiable = (bool) => {
		this.templatename=this.prjname;
        this.doSaveVisible = bool;
    }
    
    saveTemplateName =(data)=> {
		this.templatename = data;
	}

}